package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/destination")
public class DestinationController {

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyCatalogService catalogService;

    @Autowired
    private IStrategyService strategyService;


    @GetMapping("/hotRegion")
    public Object hotRegion() {
        //return regionService.queryHotRegion();//报错undefined
        return JsonResult.success(regionService.queryHotRegion());
    }


    //显示区域下的目的地
    @GetMapping("/destsByRegionId")
    public Object destsByRegionId(String regionId) {

        List<Destination> list = destinationService.getDestByRegionIdForApi(regionId);

      /*  Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("regionId", regionId);
        return JsonResult.success(map);*/
        return JsonResult.success(new ParamMap().put("list", list).put("regionId", regionId));
    }

    //点击目的地进入详情页面
    @GetMapping("/detail")
    public Object detail(String id) {
        //toasts:[],
        List<Destination> toasts = destinationService.getToasts(id);
        // dest:{},
        //Destination dest = toasts.get(toasts.size() - 1);
        //使用get方法面包屑多了一个重复的子目的地
        Destination dest = toasts.remove(toasts.size() - 1);

        //catalogs:[],
        List<StrategyCatalog> catalogs = catalogService.queryByDestId(id);

        //strategies:[],//指定目的地下的排名前三的攻略
        //page:{}
        StrategyQuery qo = new StrategyQuery();
        qo.setDestId(id);
        //分页排序
        qo.setPageable(PageRequest.of(0, 3, Sort.Direction.DESC,"viewnum"));
        Page<Strategy> page = strategyService.query(qo);


        return JsonResult.success(new ParamMap()
                .put("toasts", toasts)
                .put("dest", dest)
                .put("catalogs", catalogs)
                .put("strategies", page.getContent())//strategies只需要分页结果的内容在前台展示即可

        );
    }

    //显示地址导航（吐司）
    @GetMapping("/getToast")
    public Object getToast(String id) {
        //vue.toasts = map.toasts;
        List<Destination> toasts = destinationService.getToasts(id);
        // vue.dest = map.dest;
        Destination dest = toasts.remove(toasts.size() - 1);

        return JsonResult.success(new ParamMap()
                .put("toasts", toasts)
                .put("dest", dest));
    }


}
